/**
 * Created by Baggio on 2017-12-27.
 */
$(function () {

    var userId=$("#userId").val();
    if(userId=!'' && userId !=undefined){
        document.title="收货人信息";
    }
    $(".login_box .register_link").click(function () {
        //location.href="/user/myQRCode.html"
        $(this).parents(".login_box").hide();
        $(this).parents(".login_box").find(".user_box").find("input").val("");
        $(".register_box1 .box input").val("");
        $(".register_box1 .verification_1").val("");
        $(".register_box1").show();
    });

    $(".modifyUserBtn").click(function () {
        //var userAddress = $(".message_box5 .recipient-name .message_board_5").html();
        var userAddress = $("#userAddress").val();
        var tid = $("#tid").val();
        var userName = $("#userName").val();
        if (userAddress == undefined || userAddress == '') {
            alert("地址不能为空！");
            return;
        }
        var postData = {userAddress: userAddress,tid:tid,userName:userName};
        modifyUser(postData);
    });

    function modifyUser(postData) {
        $.ajax({
            type: "post",
            url: "/user/modifyUser.html",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                if (res.code != 1) {
                    alert(res.msg);
                    return;
                }
                alert(res.msg);
            }

        });
    }


    /*登录*/
    $(".sub1").click(function () {
        $("#login").ajaxSubmit({
            type: "POST",
            url: '/user/login.html',
            data: $('#login').serialize(),// 你的formid
            async: false,
            error: function (request) {
                alert("Connection error");
            },
            success: function (data) {

                console.log(data);
                var data = JSON.parse(data);
                if (data.code != 1) {
                    alert(data.msg);
                    return;
                }
                alert(data.msg);
                location.reload();
            }

        });

    });


    /*注册*/
    $(".register_box1 .sub2").click(function () {

        var phone = $(".register_box1").find("input[name='phone']").val();
        if (!checkMobile(phone)) {
            alert("手机号格式错误！");
            return;
        }
        var userName = $(".register_box1").find("input[name='userName']").val();
        if (isEmpty(userName)) {
            alert("用户名不能为空!");
            return;
        }
        var userAddress = $(".register_box1").find("input[name='userAddress']").val();
        if (isEmpty(userAddress)) {
            alert("用户地址不能为空!");
            return;
        }
        var passWord = $(".register_box1").find("input[name='passWord']").val();
        if (isEmpty(passWord)) {
            alert("密码不能为空!");
            return;
        }
        var passWord2 = $(".register_box1").find("input[name='passWord2']").val();
        if (isEmpty(passWord2)) {
            alert("密码2不能为空!");
            return;
        }
        if (passWord2 != passWord) {
            alert("2次密码输入不一致！");
            return;
        }

        $("#register").ajaxSubmit({
            type: "POST",
            url: '/user/regedit.html',
            data: $('#register').serialize(),// 你的formid
            async: false,
            error: function (request) {
                alert("Connection error");
            },
            success: function (data) {
                var data=JSON.parse(data);
                if (data.code != 1) {
                    showMessage(data.msg);
                    return;
                }
                alert("注册成功！");
                $(".register_box1").hide();
                location.reload();
            }
        });

        $(this).parents('.login_box').find('input').val('');
        $(this).parents('.register_box1').hide();

    });


    // 获取验证码
    var counting = false;
    $(".register_box1 .verification_2").click(function () {
        var timer, count = 60,
            that = $(this);
        var $btn = $(this), //获取验证码的按钮
            phone = $(".register_box1 input[name='phone']").val();
        console.log(phone);
        //phone = $('input[name="phone"]').val(), // 手机号码控件的值
        phoneRight = phone.match(/^[1]{1}[\d]{10}$/); // 手机号码是否符合格式
        if (!checkMobile(phone)) {
            alert("手机号格式错误！");
            return;
        }


        var postData = {phone: phone}
        $.ajax({
            type: "post",
            url: "/user/getSMSCode.html",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                if (res.code != 1) {
                    alert(res.msg);
                    return;
                }
                alert("短信已发送！");
            }

        });


        /* 倒计时 */
        /*延迟一秒问题*/
        setTimeout(function () {
            that.val(count + '(s)');
        }, 0)
        /*一分钟后再次发送*/
        timer = setInterval(function () {
            if (count > 1) {
                count--;
                // alert($(this).html())
                that.val(count + '(s)');
            } else {
                counting = false;
                count = 60;
                that.val("获取验证码");
                that.removeClass("waiting");
                clearInterval(timer);
            }
        }, 1000)


    });



})